//
//  YHYLoadSongViewController.m
//  TTMusic
//
//  Created by yanghuiyan on 16/4/1.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "YHYLoadSongViewController.h"
#import "YHYLoadOneTableViewCell.h"
#import "YHYLoadSongYunViewController.h"
#import "YHYLoadTwoTableViewCell.h"
#import "YHYLoadSongPullViewController.h"
@interface YHYLoadSongViewController ()<UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIScrollView *musicScrollView;

@property (nonatomic, strong) UISegmentedControl *segment;

@property (nonatomic, strong) TTMusicBaseTableView *musicTableView;

@property (nonatomic, strong) UIView *customView;

@end

@implementation YHYLoadSongViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"yunload"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]landscapeImagePhone:[[UIImage imageNamed:@"yunload"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightItemAction:)];
    self.navigationItem.rightBarButtonItem = rightItem;

//  创建UIScrollView
    
    self.musicScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _musicScrollView.contentSize = CGSizeMake(SysWidth * 2, SysHeight);
    _musicScrollView.contentOffset = CGPointMake(0, 0);
    _musicScrollView.delegate = self;
    _musicScrollView.pagingEnabled = YES;
    _musicScrollView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:_musicScrollView];
    
//  创建segment
    self.segment = [[UISegmentedControl alloc] initWithItems:@[@"已下载", @"正在下载"]];
    _segment.frame = CGRectMake(SysWidth / 4, 20 * FitHeight, SysWidth / 2, 40  * FitHeight);
    _segment.tintColor = [UIColor whiteColor];
    
    _segment.selectedSegmentIndex = 0;
    [_segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = _segment;
    
// 创建tableView
    self.musicTableView = [[TTMusicBaseTableView alloc] initWithFrame:CGRectMake(0, 0, self.musicScrollView.frame.size.width, self.musicScrollView.frame.size.height) style:UITableViewStylePlain];
    [self.musicTableView registerClass:[YHYLoadOneTableViewCell class] forCellReuseIdentifier:@"YHYLoadOneTableViewCellIdentifier"];
    [self.musicTableView registerClass:[YHYLoadTwoTableViewCell class] forCellReuseIdentifier:@"YHYLoadTwoTableViewCellIdentifier"];
    self.musicTableView.delegate = self;
    self.musicTableView.dataSource = self;
    [self.musicScrollView addSubview:self.musicTableView];
    
//    pullButton 点击
    self.customView = [[UIView alloc] initWithFrame:CGRectMake(0, SysHeight, SysWidth, SysHeight)];
    self.customView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.customView];

    
    UIView *upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SysWidth,SysHeight/2-50)];
    upView.backgroundColor = [UIColor lightGrayColor];
    [upView setAlpha:0.5];
    [self.customView addSubview:upView];
    
    // 给upView添加手势
    UITapGestureRecognizer *upTap = [[UITapGestureRecognizer alloc] init];
    [upTap addTarget:self action:@selector(upTapAction:)];
    [upView addGestureRecognizer:upTap];
    
    
    UIView *halfView = [[UIView alloc] initWithFrame:CGRectMake(0, SysHeight / 2 - 50, SysWidth, SysHeight/2 + 50)];
    halfView.backgroundColor = [UIColor whiteColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 295, halfView.frame.size.width, 60);
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    button.layer.borderWidth = 1;
    button.layer.borderColor = [UIColor lightGrayColor].CGColor;
    button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [button setTintColor:[UIColor grayColor]];
    [button setTitle:@"取   消" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//    button.backgroundColor = [UIColor redColor];
    [halfView addSubview:button];
    [self.customView addSubview:halfView];
    
}

- (void)rightItemAction:(UIBarButtonItem *) sender {
    YHYLoadSongYunViewController *yunVC = [[YHYLoadSongYunViewController alloc] init];
    [self.navigationController pushViewController:yunVC animated:YES];
}

- (void)upTapAction:(UITapGestureRecognizer *) sender {
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.customView.frame = CGRectMake(0, SysHeight, SysWidth, SysHeight);
        
    }];
    
}

- (void)buttonAction:(UIButton *)sender {
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.customView.frame = CGRectMake(0, SysHeight, SysWidth, SysHeight);
        
    }];

    
}

#pragma mark - tableView代理方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
       YHYLoadOneTableViewCell *cell = [self.musicTableView dequeueReusableCellWithIdentifier:@"YHYLoadOneTableViewCellIdentifier"];
        return cell;
    }
      YHYLoadTwoTableViewCell *cell = [self.musicTableView dequeueReusableCellWithIdentifier:@"YHYLoadTwoTableViewCellIdentifier"];
    
    [cell.pullButton addTarget:self action:@selector(pullButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

- (void)pullButtonAction:(UIButton *)sender {

    [UIView animateWithDuration:0.5 animations:^{
        
        
        self.customView.frame = CGRectMake(0, 0, SysWidth, SysHeight);
        
    }];
    
    
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.musicTableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - segment控制scrollView
- (void)segmentAction:(UISegmentedControl *)sender {
    
        self.musicScrollView.contentOffset = CGPointMake(SysWidth * sender.selectedSegmentIndex, 0);
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (self.musicScrollView.contentOffset.x == 0) {
        self.segment.selectedSegmentIndex = 0;
    }else if(self.musicScrollView.contentOffset.x == SysWidth) {
        self.segment.selectedSegmentIndex = 1;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
